package cc.rico.es;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;

import java.io.IOException;

/**
 * @Author rico
 * @Date 2019-09-22 16:41
 * @Version 1.0
 * @Description
 */
@Slf4j
public class ElasticSearchRestClient {

    public static RestClient getSimpleInstance() {
        RestClient restClient = RestClient.builder(new HttpHost("192.168.223.140", 9200, "http")).build();
        return restClient;
    }


    public static RestClient getInstance() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.223.140", 9200, "http"))
                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                        return builder.setSocketTimeout(60000)
                                .setConnectTimeout(5000);
                    }
                })
                .setHttpClientConfigCallback(httpAsyncClientBuilder ->
                    httpAsyncClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build())
                );

        return builder.build();
    }

    public static void shutdown(RestClient restClient) throws IOException {
        restClient.close();
    }



}
